/**
 * Created by moc on 2016/3/30.
 */

jQuery(document).ready(function ($) {
    var ws = new WebSocket("ws://blog.mocyx.com:10001");
    ws.onopen = function () {
        // Web Socket is connected, send data using send()
        console.log("ws open");
    };
    ws.onmessage = function (evt) {
        var received_msg = evt.data;
        console.log(received_msg);
        var data = JSON.parse(received_msg)
        var html = '<div class="chat-msg"><div class="headimg"><img height="50px" width="50px" src="http://wx.qlogo.cn/mmopen/ajNVdqHZLLCKHtxna05BmrtacQCiavGphUDjlqFpkzPot4VT0KWgXawKsQUvI2Aj5Ucgl9FHt6c9Wia5YZfpVeqg/0"></div><div class="usermsg"><p>hello world</p></div></div>'
        var obj = $(html)
        obj.find('img').attr('src',data.user.headimgurl)
        obj.find('p').html(data.text)
        $('#chatFrame').prepend(obj)

    };
    ws.onclose = function () {
        // websocket is closed.
        console.log("Connection is closed...");
    };
    ws.onerror = function (evt) {
        // websocket is closed.
        console.log("Connection is closed...");
        console.log(evt);
    };

})